package tests;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import common.Utils;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

public class CreateActivityPage extends Utils {

    private static String url = "http://82.156.185.199:8080/admin.html";

    public CreateActivityPage() {
        super(url);
    }

    /**
     * 创建活动页面元素
     */
    public void pageTest(){
        // 切换回主页面（关键！）
        driver.switchTo().defaultContent();
        driver.findElement(By.cssSelector("#createActivity")).click();
        driver.switchTo().frame(0);
        // "创建抽奖活动"
        driver.findElement(By.cssSelector("body > div.container > h2"));
        // "活动名称"
        driver.findElement(By.cssSelector("#activityForm > div:nth-child(1) > label"));
        // "活动名称" 输入框
        driver.findElement(By.cssSelector("#activityName"));
        // "活动描述"
        driver.findElement(By.cssSelector("#activityForm > div.form-group.desc-row > label"));
        // "活动描述" 输入框
        driver.findElement(By.cssSelector("#description"));
        // "圈选奖品" 按钮
        driver.findElement(By.cssSelector("#buttonPrizes"));
        // 点击按钮
        driver.findElement(By.cssSelector("#buttonPrizes")).click();
        // "奖品列表"
        driver.findElement(By.xpath("//*[@id=\"prizesModal\"]/div/h2"));
        // X
        driver.findElement(By.cssSelector("#prizesModal > div > h2 > span")).click();

        // "圈选人员" 按钮
        driver.findElement(By.cssSelector("#buttonUsers"));
        driver.findElement(By.cssSelector("#buttonUsers")).click();
        driver.findElement(By.xpath("//*[@id=\"usersModal\"]/div/h2"));
        driver.findElement(By.cssSelector("#usersModal > div > h2 > span")).click();

        // "创建活动" 按钮
        driver.findElement(By.cssSelector("#createActivity"));

        System.out.println("创建活动页面元素————成功！");
    }

    /**
     * 创建失败
     */
    public void createActivityFail(){
        // 1. 什么都不填写 + 什么都不圈选，点击 "创建" 按钮
        driver.findElement(By.cssSelector("#createActivity")).click();
        // 提示
        driver.findElement(By.cssSelector("#activityName-error"));
        driver.findElement(By.cssSelector("#description-error"));

        // 2. 填写活动名称和活动描述 + 什么都不圈选，点击“创建”按钮
        driver.findElement(By.cssSelector("#activityName")).sendKeys("测10");
        driver.findElement(By.cssSelector("#description")).sendKeys("测10");
        driver.findElement(By.cssSelector("#createActivity")).click();
        // 处理弹窗
        wait.until(ExpectedConditions.alertIsPresent());
        String expectText1 = "请至少选择一个奖品";
        Alert alert1 = driver.switchTo().alert();
        String actualText1 = alert1.getText();
        Assertions.assertEquals(expectText1, actualText1);
        alert1.accept();

        // 3. 填写活动名称和活动描述 + 圈选奖品，点击 "创建" 按钮
        driver.findElement(By.cssSelector("#buttonPrizes")).click();
        // 勾选两个
        driver.findElement(By.cssSelector("#prize-26")).click();
        driver.findElement(By.cssSelector("#prize-25")).click();
        // 确定
        driver.findElement(By.cssSelector("#prizesModal > div > div.form-btn-box > button.btn.btn-primary")).click();
        // 创建活动
        driver.findElement(By.cssSelector("#createActivity")).click();
        // 处理弹窗
        String expectText2 = "请至少选择一个人员, 人员数量应大于等于奖品总量";
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert2 = driver.switchTo().alert();
        String actualText2 = alert2.getText();
        Assertions.assertEquals(expectText2, actualText2);
        alert2.accept();


        // 4. 圈选奖品数 **大于** 圈选人员数，点击“创建”按钮
        // 圈选人员
        driver.findElement(By.cssSelector("#buttonUsers")).click();
        driver.findElement(By.cssSelector("#user-53")).click();
        driver.findElement(By.cssSelector("#usersModal > div > div.form-btn-box > button.btn.btn-primary")).click();
        // 点击创建
        driver.findElement(By.cssSelector("#createActivity")).click();
        // 处理弹窗
        String expectText3 = "创建失败！活动关联的奖品及人员数量设置异常！";
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert3 = driver.switchTo().alert();
        String actualText3 = alert3.getText();
        Assertions.assertEquals(expectText3, actualText3);
        alert3.accept();

        System.out.println("创建活动失败————成功！");
    }

    /**
     * 创建成功
     */
    public void createActivitySuc(){
        // 再圈选一个人员即可
        driver.findElement(By.cssSelector("#buttonUsers")).click();
        driver.findElement(By.cssSelector("#user-52")).click();
        driver.findElement(By.cssSelector("#usersModal > div > div.form-btn-box > button.btn.btn-primary")).click();
        driver.findElement(By.cssSelector("#createActivity")).click();
        String expectText = "创建成功!";
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        String actualText = alert.getText();
        Assertions.assertEquals(expectText, actualText);
        alert.accept();
    }
}
